package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mapper.ManagerUserMapper;
import com.itany.pojo.ManagerUser;
import com.itany.service.ManagerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2019-09-18 上午9:59
 * Description：<描述>
 */

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
public class ManagerUserServiceImpl implements ManagerUserService {

    @Autowired
    private ManagerUserMapper managerUserMapper;

    @Transactional(readOnly = true)
    public ManagerUser findByUserNameAndPassword(ManagerUser managerUser) {

        ManagerUser user= managerUserMapper.selectByUserNameAndPassword(managerUser);


        return user;
    }
    @Transactional(readOnly = true)
    public PageInfo<ManagerUser> findAll(Integer page, Integer rows, ManagerUser managerUser) {
        PageHelper.startPage(page, rows);
        List<ManagerUser> managerUsers = managerUserMapper.selectAll(managerUser);
        PageInfo<ManagerUser> info = new PageInfo<ManagerUser>(managerUsers);
        return info;
    }

    /**
     * User添加
     * @param managerUser
     * @param ids
     */
    public void addUser(ManagerUser managerUser, String ids) {

            managerUserMapper.insertUser(managerUser);

    }

    /**
     * Role添加
     * @param id
     * @param ids
     */
    public void addRoleById(Integer id, String ids) {
        String[] split = ids.split(",");

        for(int i=0;i<split.length;i++){

            managerUserMapper.insertRoleById(id,Integer.parseInt(split[i]));


        }
    }

    public void modifyUser(ManagerUser managerUser, String ids) {

        managerUserMapper.updateUserById(managerUser);

        //删除角色
        managerUserMapper.deleteRoleByUserId(managerUser.getId());

        //角色添加
        String[] split = ids.split(",");

        for(int i=0;i<split.length;i++){
            managerUserMapper.insertRoleById(managerUser.getId(),Integer.parseInt(split[i]));

        }
    }
}